package com.ibeeking.found.cop.b.rest.service.impl;

import com.ibeeking.found.common.constants.GlobalConstant;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.ICopSourceGroupService;
import com.ibeeking.found.cop.b.rest.service.ICopSourceService;
import com.ibeeking.found.cop.b.rest.util.DoUtils;
import com.ibeeking.found.cop.service.common.bo.CopSourceBO;
import com.ibeeking.found.cop.service.common.constants.SourceConstant;
import com.ibeeking.found.cop.service.common.dos.CopSourceGroupDO;
import com.ibeeking.found.cop.service.common.param.CopSourceGroupParam;
import com.ibeeking.found.cop.service.common.query.CopSourceGroupQuery;

import com.ibeeking.found.cop.service.common.vo.CopSourceGroupListVO;
import com.ibeeking.found.cop.service.common.vo.CopSourceGroupVO;
import com.ibeeking.found.cop.service.mapper.CopSourceGroupMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.data.StringUtils;
import com.ibeeking.nematos.utils.data.TreeFastUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @ClassName CopSourceGroupServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-23 15:10
 **/
@Service
public class CopSourceGroupServiceImpl extends ServiceImpl<CopSourceGroupMapper, CopSourceGroupDO> implements ICopSourceGroupService {

    @Resource
    private CopSourceGroupMapper copSourceGroupMapper;

    @Resource
    private ICopSourceService copSourceServiceImpl;

    @Override
    public List<CopSourceGroupListVO> list(CopSourceGroupQuery query){
        LambdaQueryWrapper<CopSourceGroupDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(query.getShopId()!=null, CopSourceGroupDO::getShopId, query.getShopId())
                .eq(CopSourceGroupDO::getMerchantsBusiness, query.getMerchantsBusiness())
                .eq(CopSourceGroupDO::getType, query.getType())
                .like(StringUtils.isNotBlank(query.getName()), CopSourceGroupDO::getName, query.getName())
                .orderByDesc(CopSourceGroupDO::getSystemStatus)
                .orderByAsc(CopSourceGroupDO::getSort)
                .orderByDesc(CopSourceGroupDO::getCreateTime);
        queryWrapper.or(Wrapper-> Wrapper.eq(CopSourceGroupDO::getSystemStatus,1));
        List<CopSourceGroupDO> copSourceGroupDOS = copSourceGroupMapper.selectList(queryWrapper);
        List<CopSourceGroupListVO> copSourceGroupListVOS = JsonUtils.jsonToList(copSourceGroupDOS, CopSourceGroupListVO.class);
        for (CopSourceGroupListVO copSourceGroupListVO : copSourceGroupListVOS) {
            CopSourceBO copSourceBO = new CopSourceBO();
            if(query.getShopId()!=null){
                copSourceBO.setId(copSourceGroupListVO.getId());
                copSourceBO.setMerchantsBusiness(1);
                copSourceBO.setType(query.getType());
                copSourceBO.setShopId(query.getShopId());
            }else{
                copSourceBO.setId(copSourceGroupListVO.getId());
                copSourceBO.setMerchantsBusiness(2);
                copSourceBO.setType(query.getType());

            }
            copSourceGroupListVO.setSourceCount(copSourceServiceImpl.listCountByParentIdAndType(copSourceBO));
        }
        copSourceGroupListVOS.forEach(copSourceGroupListVO -> {
            if(SourceConstant.all_parentId.equals(copSourceGroupListVO.getId())){
                copSourceGroupListVO.setSourceCount(copSourceGroupListVOS.stream().mapToInt(CopSourceGroupListVO::getSourceCount).sum());
            }
        });
        return TreeFastUtils.listToTree(copSourceGroupListVOS, GlobalConstant.PARENT_ID);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(CopSourceGroupParam param){
        CopSourceGroupDO copSourceGroupDO = BeanUtil.convertBean(param, CopSourceGroupDO.class);
        copSourceGroupDO.setCreateBy(UserUtils.getUserId());
        copSourceGroupDO.setCreateTime(LocalDateTime.now());
        copSourceGroupDO.setModifyTime(LocalDateTime.now());
        LambdaQueryWrapper<CopSourceGroupDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(param.getShopId()!=null, CopSourceGroupDO::getShopId, param.getShopId())
                    .eq(CopSourceGroupDO::getMerchantsBusiness, param.getMerchantsBusiness())
                    .eq(CopSourceGroupDO::getType, param.getType())
                    .eq(CopSourceGroupDO::getName, param.getName());
        List<CopSourceGroupDO> repeatList = copSourceGroupMapper.selectList(queryWrapper);
        if(Collections3.isNotEmpty(repeatList)){
            throw new BusinessException("分组名重复");
        }
        return copSourceGroupMapper.insert(copSourceGroupDO)>0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(CopSourceGroupParam param){
        CopSourceGroupDO copSourceGroupDO1 = copSourceGroupMapper.selectById(param.getId());

        if(copSourceGroupDO1.getSystemStatus().equals(1)){
            throw new BusinessException("系统分组禁止修改");
        }
        CopSourceGroupDO copSourceGroupDO = BeanUtil.convertBean(param, CopSourceGroupDO.class);
        copSourceGroupDO.setModifyBy(UserUtils.getUserId());
        copSourceGroupDO.setModifyTime(LocalDateTime.now());
        List<CopSourceGroupDO> repeatList = DoUtils.getRepeatList(copSourceGroupDO, copSourceGroupMapper, param.getId(), param.getName(),null);
        repeatList = repeatList.stream().filter(node -> node.getId().equals(param.getId())).collect(Collectors.toList());
        if(Collections3.isNotEmpty(repeatList)){
            throw new BusinessException("分组名重复");
        }
        return copSourceGroupMapper.updateById(copSourceGroupDO)>0;
    }

    @Override
    public CopSourceGroupVO view(Long id){
        CopSourceGroupDO copSourceGroupDO = copSourceGroupMapper.selectById(id);
        return BeanUtil.convertBean(copSourceGroupDO, CopSourceGroupVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean del(Long id){
        CopSourceGroupDO copSourceGroupDO = copSourceGroupMapper.selectById(id);
        if(copSourceGroupDO.getSystemStatus().equals(1)){
            throw new BusinessException("系统分组禁止删除");
        }
        return this.delMethod(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delMethod(Long id) {
        LambdaQueryWrapper<CopSourceGroupDO> queryWrapper= new LambdaQueryWrapper<>();
        queryWrapper.eq(CopSourceGroupDO::getParentId, id);
        List<CopSourceGroupDO> copSourceGroupDOS = copSourceGroupMapper.selectList(queryWrapper);
        if(Collections3.isNotEmpty(copSourceGroupDOS)){
            for (CopSourceGroupDO copSourceGroupDO : copSourceGroupDOS) {
                this.del(copSourceGroupDO.getId());
            }
        }
        //删除分组将素材修改到未分组
        copSourceServiceImpl.modifyParentId(id);
        return copSourceGroupMapper.deleteById(id)>0;
    }
}